import 'package:app/common/global.dart';
import 'package:app/common/shop_cart_card.dart';
import 'package:app/model/product_sale_out.dart';
import 'package:app/model/product_view_model.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';



class ShopCartPage extends StatefulWidget {
  ShopCartPage({Key key}) : super(key: key);

  @override
  ShopCartState createState() => new ShopCartState();
}
class ShopCartState extends State<ShopCartPage> with SingleTickerProviderStateMixin  {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new MaterialApp(
        title: 'shopcart',
        theme: new ThemeData(
          primaryColor: Global.currentTheme,
          fontFamily: 'Roboto',
        ),
        home:Center(
          child: new Scaffold(
            appBar: AppBar(
              title:Text('计价'),
              leading:IconButton(icon: Icon(Icons.arrow_back_ios), onPressed: ()=>Navigator.pop(context)),
                actions: <Widget>[
                  Consumer<GlobalData>(
                      builder:(context,globalData,_) =>GestureDetector(
                        //behavior: HitTestBehavior.opaque,
                         child: Icon(Icons.remove_shopping_cart),
                         onTap:  () => Provider.of<GlobalData>(context).clearShopCartCount()
                      )),
                   //Padding(padding: EdgeInsets.only(top: 50),),
                   Consumer<GlobalData>(builder:(context,globalData,_) =>Padding(
                    padding: const EdgeInsets.only(top:8.0),
                    child: Text(globalData.shopCartCount.toString()),
                  )),
                  Padding(padding: EdgeInsets.only(right: 20),)
                ],
            ),
            body:Consumer<GlobalData>(
              builder:(context,globalData,_) =>ListView.separated(
                itemCount: globalData.listProduct.length,
                itemBuilder: (context, index) {
                  return new ProductShopCard(data:
                  new ProductViewModel(id:globalData.listProduct[index].id,productName:globalData.listProduct[index].productName,
                      priceSale:globalData.listProduct[index].priceSale,priceBig:globalData.listProduct[index].priceBig==null?"0":globalData.listProduct[index].priceBig,
                      img:globalData.listProduct[index].img));
                },
                separatorBuilder: (context, index) {
                  return Divider(
                    height: 1,
                    indent: 75,
                    color: Color(0xFFDDDDDD),
                  );
                },
              ),
            ),
            bottomNavigationBar: Container(
              height: 60,
              child: Consumer<GlobalData>(
                builder:(context,globalData,_)
                {
                  var calc = globalData.calc();
                  return Container(
                    color: globalData.currentTheme ,
                    child: new Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: <Widget>[
                      Padding(padding: EdgeInsets.only(left: 2)),
                      Center(child: Text("数量: ${globalData.listProduct.length}",style: TextStyle(color: Colors.white,fontWeight: FontWeight.w500))),
                      Center(child: Text("总代理价: ${calc["priceBig"]}",style: TextStyle(color: Colors.white,fontWeight: FontWeight.w500))),
                      Center(child: Text("总售价: ${calc["priceSale"]}",style: TextStyle(color: Colors.white,fontWeight: FontWeight.w500))),
                      Center(child: Text("净利润: ${calc["money"]}",style: TextStyle(color: Colors.white,fontWeight: FontWeight.w500))),
                      Center(child: FlatButton(child:Text("售出",style: TextStyle(color: Colors.white,fontWeight: FontWeight.w500)),color:Colors.black38,
                          onPressed:()=>globalData.saleOut(new ProductSaleOut(dateTime:DateTime.now().toString().substring(0,16),content:calc["content"],priceBig:calc["priceBig"] ,priceSale:calc["priceSale"] ,money:calc["money"])))),
                      Padding(padding: EdgeInsets.only(right: 2)),
                    ],
                ),
                  );
                }
              ),
            ),
          ),
        ),
    );
  }

}
